/*
 * @Author: sunli
 * @Date: 2024-02-21 10:35:21
 * @LastEditors: sunli
 * @LastEditTime: 2024-02-21 14:58:58
 * @FilePath: /debug-server/router/login.js
 * @Description: 
 */
let express = require('express')
let router = express()
let mongoose = require('mongoose')
let userModel = mongoose.model('user')
router.post('/', async(req, res) => {
  const body = req.body

  // 完整性校验
  if (!body.userName || !body.password) {
    return res.send({code: 0,message: '未完善注册信息'})
  }

  const checkName = await userModel.findOne({userName: body.userName, password: body.password})
  const checkMobile = await userModel.findOne({mobile: body.userName, password: body.password})
  const checkEmail = await userModel.findOne({email: body.userName, password: body.password})
  if (checkName || checkMobile || checkEmail) {
    const findUser = checkName || checkMobile || checkEmail
    res.send({
      code: 1,
      message: '登录成功',
      resObject: {
        userName: findUser.userName,
        email: findUser.email,
        mobile: findUser.mobile
      }
    })
  }
  else {
    res.send({
      code: 0,
      message: '信息错误, 登录失败'
    })
  }
  
})

module.exports = router
